package com.AUI
{
	import com.AUI.define.style.*;
	/**
	 * Css Parser.
	 */
	public class ACssParser
	{
		/**
		 * Parse.
		 */
		public static function parse(content:String,dest:ACss):void
		{
			// format
			content = format(content);
			// fetch selector
			var styles:Array = content.split('}');
			styles.pop();
			for(var i:int = 0,lenI:int = styles.length;i < lenI;++i)
			{
				var style:String = styles[i];
				var fields:Array = style.split('{');
				var names:Array  = fields[0].split(',');
				var attrs:Array  = fields[1].split(';');
				attrs.pop();
				for(var j:int = 0,lenJ:int = names.length;j < lenJ;++j)
				{
					dest.addStyle(names[j],convertStyle(attrs));
				}
			}
		}
		/**
		 * Format.
		 */
		public static function format(content:String):String
		{
			content = content.replace(/\/\/.+/g,'');
			content = content.replace(/^\s+|\s+$/g,'');
			content = content.replace(/\s*{\s*/g,'{');
			content = content.replace(/\s*}\s*/g,'}');
			content = content.replace(/\s*:\s*/g,':');
			content = content.replace(/\s*;\s*/g,';');
			content = content.replace(/\s*=\s*/g,'=');
			return content;
		}
		/**
		 * Convert style to style attribute set.
		 */
		public static function convertStyle(style:Array):Vector.<ACssStyleAttr>
		{
			var attrs:Vector.<ACssStyleAttr> = new Vector.<ACssStyleAttr>;
			var vList:Array;
			var vVar0:*;
			var vVar1:*;
			var vVar2:*;
			var vVar3:*;
			for(var i:int = 0,len:int = style.length;i < len;++i)
			{
				var attr:Array = style[i].split(':');
				var k:String = attr[0];
				var v:String = attr[1];
				switch(k)
				{
					//================================
					//
					//	单一样式
					//
					//================================
					// [布尔型]
					case 'font-bold':
					case 'font-italic':
					case 'font-underline':
					case 'background-repeat':
						attrs.push(new ACssStyleAttr(k,v == 'true' ? true : false));
						break;
					// [字符串]
					case 'font-align':
					case 'font-family':
					case 'font-placement':
						attrs.push(new ACssStyleAttr(k,v));
						break;
					// [浮点]
					case 'border-top-alpha':
					case 'border-right-alpha':
					case 'border-bottom-alpha':
					case 'border-left-alpha':
					case 'alpha':
					case 'background-solid-alpha':
						attrs.push(new ACssStyleAttr(k,parseFloat(v)));
						break;
					// [颜色]
					case 'border-top-color':
					case 'border-right-color':
					case 'border-bottom-color':
					case 'border-left-color':	
					case 'font-color':
					case 'background-solid-color':
						attrs.push(new ACssStyleAttr(k,parseInt(v.substr(1),16)));
						break;
					// [十进制整数]
					case 'border-top-width':
					case 'border-bottom-width':
					case 'border-left-width':
					case 'border-right-width':
					case 'margin-top':
					case 'margin-right':
					case 'margin-bottom':
					case 'margin-left':
					case 'padding-top':
					case 'padding-bottom':
					case 'padding-right':
					case 'padding-left':
					case 'font-size':
					case 'gap-h':
					case 'gap-v':
						attrs.push(new ACssStyleAttr(k,parseInt(v,10)));
						break
					// [图片]
					case 'background-image':
					case 'icon-image':
						attrs.push(new ACssStyleAttr(k,ACss.getImage(v)));
						break;
					// [特别]
					case 'align-h':
						if(v == 'center')
						{
							attrs.push(new ACssStyleAttr(k,0));
						}
						else if(v == 'right')
						{
							attrs.push(new ACssStyleAttr(k,1));
						}
						else
						{
							attrs.push(new ACssStyleAttr(k,-1));
						}
						break;
					case 'align-v':
						if(v == 'center')
						{
							attrs.push(new ACssStyleAttr(k,0));
						}
						else if(v == 'bottom')
						{
							attrs.push(new ACssStyleAttr(k,1));
						}
						else
						{
							attrs.push(new ACssStyleAttr(k,-1));
						}
						break;
					case 'background-color':
						if(v == 'transparent')
						{
							attrs.push(new ACssStyleAttr('background-solid-alpha',0));
							attrs.push(new ACssStyleAttr('background-solid-color',0));
						}
						else
						{
							attrs.push(new ACssStyleAttr('background-solid-alpha',1));
							attrs.push(new ACssStyleAttr('background-solid-color',parseInt(v.substr(1),16)));
						}
						break;
					case 'icon-color':
						if(v == 'transparent')
						{
							attrs.push(new ACssStyleAttr('icon-solid-alpha',0));
							attrs.push(new ACssStyleAttr('icon-solid-color',0));
						}
						else
						{
							attrs.push(new ACssStyleAttr('icon-solid-alpha',1));
							attrs.push(new ACssStyleAttr('icon-solid-color',parseInt(v.substr(1),16)));
						}
						break;
					case 'width':
						if(v == 'auto')
						{
							attrs.push(new ACssStyleAttr('content-width-type',DIM_TYPE.AUTO));
						}
						else
						{
							attrs.push(new ACssStyleAttr('content-width-type',DIM_TYPE.PX));
							attrs.push(new ACssStyleAttr('content-width-value',parseInt(v)));
						}
						break;
					case 'height':
						if(v == 'auto')
						{
							attrs.push(new ACssStyleAttr('content-height-type',DIM_TYPE.AUTO));
						}
						else
						{
							attrs.push(new ACssStyleAttr('content-height-type',DIM_TYPE.PX));
							attrs.push(new ACssStyleAttr('content-height-value',parseInt(v)));
						}
						break;
					//================================
					//
					//	复合样式
					//
					//================================
					case 'padding':
						vList = v.split(' ');
						switch(vList.length)
						{
							case 1:
								vVar0 = parseInt(vList[0]);
								attrs.push(new ACssStyleAttr('padding-top',	vVar0));
								attrs.push(new ACssStyleAttr('padding-right',	vVar0));
								attrs.push(new ACssStyleAttr('padding-bottom',vVar0));
								attrs.push(new ACssStyleAttr('padding-left',	vVar0));
								break;
							case 2:
								vVar0 = parseInt(vList[0]);
								vVar1 = parseInt(vList[1]);
								attrs.push(new ACssStyleAttr('padding-top',	vVar0));
								attrs.push(new ACssStyleAttr('padding-bottom',vVar0));
								attrs.push(new ACssStyleAttr('padding-right',	vVar1));
								attrs.push(new ACssStyleAttr('padding-left',	vVar1));
								break;
							case 4:
								vVar0 = parseInt(vList[0]);
								vVar1 = parseInt(vList[1]);
								vVar2 = parseInt(vList[2]);
								vVar3 = parseInt(vList[3]);
								attrs.push(new ACssStyleAttr('padding-top',	vVar0));
								attrs.push(new ACssStyleAttr('padding-right',	vVar1));
								attrs.push(new ACssStyleAttr('padding-bottom',vVar2));
								attrs.push(new ACssStyleAttr('padding-left',	vVar3));
								break;
						}
						break;
					case 'margin':
						vList = v.split(' ');
						switch(vList.length)
						{
							case 1:
								vVar0 = parseInt(vList[0]);
								attrs.push(new ACssStyleAttr('margin-top',		vVar0));
								attrs.push(new ACssStyleAttr('margin-right',	vVar0));
								attrs.push(new ACssStyleAttr('margin-bottom',	vVar0));
								attrs.push(new ACssStyleAttr('margin-left',	vVar0));
								break;
							case 2:
								vVar0 = parseInt(vList[0]);
								vVar1 = parseInt(vList[1]);
								attrs.push(new ACssStyleAttr('margin-top',		vVar0));
								attrs.push(new ACssStyleAttr('margin-bottom',	vVar0));
								attrs.push(new ACssStyleAttr('margin-right',	vVar1));
								attrs.push(new ACssStyleAttr('margin-left',	vVar1));
								break;
							case 4:
								vVar0 = parseInt(vList[0]);
								vVar1 = parseInt(vList[1]);
								vVar2 = parseInt(vList[2]);
								vVar3 = parseInt(vList[3]);
								attrs.push(new ACssStyleAttr('margin-top',		vVar0));
								attrs.push(new ACssStyleAttr('margin-right',	vVar1));
								attrs.push(new ACssStyleAttr('margin-bottom',	vVar2));
								attrs.push(new ACssStyleAttr('margin-left',	vVar3));
								break;
						}
						break;
					case 'background-slice':
						vList = v.split(' ');
						vVar0 = parseInt(vList[0]);
						vVar1 = parseInt(vList[1]);
						vVar2 = parseInt(vList[2]);
						vVar3 = parseInt(vList[3]);
						attrs.push(new ACssStyleAttr('background-slice-x',	  vVar0));
						attrs.push(new ACssStyleAttr('background-slice-y',	  vVar1));
						attrs.push(new ACssStyleAttr('background-slice-width', vVar2));
						attrs.push(new ACssStyleAttr('background-slice-height',vVar3));
						break;
					case 'border-width':
						vList = v.split(' ');
						switch(vList.length)
						{
							case 1:
								vVar0 = parseInt(vList[0]);
								attrs.push(new ACssStyleAttr('border-top-width',		vVar0));
								attrs.push(new ACssStyleAttr('border-right-width',	vVar0));
								attrs.push(new ACssStyleAttr('border-bottom-width',	vVar0));
								attrs.push(new ACssStyleAttr('border-left-width',		vVar0));
								break;
							case 2:
								vVar0 = parseInt(vList[0]);
								vVar1 = parseInt(vList[1]);
								attrs.push(new ACssStyleAttr('border-top-width',		vVar0));
								attrs.push(new ACssStyleAttr('border-bottom-width',	vVar0));
								attrs.push(new ACssStyleAttr('border-right-width',	vVar1));
								attrs.push(new ACssStyleAttr('border-left-width',		vVar1));
								break;
							case 4:
								vVar0 = parseInt(vList[0]);
								vVar1 = parseInt(vList[1]);
								vVar2 = parseInt(vList[2]);
								vVar3 = parseInt(vList[3]);
								attrs.push(new ACssStyleAttr('border-top-width',		vVar0));
								attrs.push(new ACssStyleAttr('border-right-width',	vVar1));
								attrs.push(new ACssStyleAttr('border-bottom-width',	vVar2));
								attrs.push(new ACssStyleAttr('border-left-width',		vVar3));
								break;
						}
						break;
					case 'border-color':
						vList = v.split(' ');
						switch(vList.length)
						{
							case 1:
								vVar0 = parseInt(vList[0].substr(1),16);
								attrs.push(new ACssStyleAttr('border-top-color',		vVar0));
								attrs.push(new ACssStyleAttr('border-right-color',	vVar0));
								attrs.push(new ACssStyleAttr('border-bottom-color',	vVar0));
								attrs.push(new ACssStyleAttr('border-left-color',		vVar0));
								break;
							case 2:
								vVar0 = parseInt(vList[0].substr(1),16);
								vVar1 = parseInt(vList[1].substr(1),16);
								attrs.push(new ACssStyleAttr('border-top-color',		vVar0));
								attrs.push(new ACssStyleAttr('border-bottom-color',	vVar0));
								attrs.push(new ACssStyleAttr('border-right-color',	vVar1));
								attrs.push(new ACssStyleAttr('border-left-color',		vVar1));
								break;
							case 4:
								vVar0 = parseInt(vList[0].substr(1),16);
								vVar1 = parseInt(vList[1].substr(1),16);
								vVar2 = parseInt(vList[2].substr(1),16);
								vVar3 = parseInt(vList[3].substr(1),16);
								attrs.push(new ACssStyleAttr('border-top-color',		vVar0));
								attrs.push(new ACssStyleAttr('border-right-color',	vVar1));
								attrs.push(new ACssStyleAttr('border-bottom-color',	vVar2));
								attrs.push(new ACssStyleAttr('border-left-color',		vVar3));
								break;
						}
						break;
					case 'border-alpha':
						vList = v.split(' ');
						switch(vList.length)
						{
							case 1:
								vVar0 = parseFloat(vList[0]);
								attrs.push(new ACssStyleAttr('border-top-alpha',		vVar0));
								attrs.push(new ACssStyleAttr('border-right-alpha',	vVar0));
								attrs.push(new ACssStyleAttr('border-bottom-alpha',	vVar0));
								attrs.push(new ACssStyleAttr('border-left-alpha',		vVar0));
								break;
							case 2:
								vVar0 = parseFloat(vList[0]);
								vVar1 = parseFloat(vList[1]);
								attrs.push(new ACssStyleAttr('border-top-alpha',		vVar0));
								attrs.push(new ACssStyleAttr('border-bottom-alpha',	vVar0));
								attrs.push(new ACssStyleAttr('border-right-alpha',	vVar1));
								attrs.push(new ACssStyleAttr('border-left-alpha',		vVar1));
								break;
							case 4:
								vVar0 = parseFloat(vList[0]);
								vVar1 = parseFloat(vList[1]);
								vVar2 = parseFloat(vList[2]);
								vVar3 = parseFloat(vList[3]);
								attrs.push(new ACssStyleAttr('border-top-alpha',		vVar0));
								attrs.push(new ACssStyleAttr('border-right-alpha',	vVar1));
								attrs.push(new ACssStyleAttr('border-bottom-alpha',	vVar2));
								attrs.push(new ACssStyleAttr('border-left-alpha',		vVar3));
								break;
						}
						break;
					default:
						trace(k,v);
						throw new Error('unsupport item');
				}
			}
			return attrs;
		}
	}
}